local Player = {}
local Screenwide = ScrW()
local Screenheigh = ScrH()
local Xstartposition = 0
local Ystartposition = Screenheigh/8*7
local Wide = Screenwide
local Heigh = Screenheigh/8
local Fontheigh = 16
local xmenustart = 2
local ymenustart = ScrH()/8
local menuwide = ScrW()/3 - 2
local menuheigh = ScrH()/8 * 5
local lineheigh = 20
local buttonheigh = lineheigh - 2
local xclosebuttonstart = xmenustart + menuwide/4
local xclosebuttonwide = menuwide/4*2
local xnamestart = xmenustart
local xnamewide = menuwide/2 -4
local xbuttonwide = menuwide/2/3 -4
local xbuttonfriendlystart = xnamestart+xnamewide
local xbuttonneutralstart = xbuttonfriendlystart + xbuttonwide +2
local xbuttonenemystart = xbuttonneutralstart + xbuttonwide +2
local yline1 = 5
local song = ""
surface.CreateFont("Tahoma", 10, 800, true, false, "RadarClassNames")
surface.CreateFont("Tahoma", 12, 800, true, false, "RadarCompass")
local MOTD = {}
menu = {}
local traderlist = {}
traderlist.list = {}
traderlist.header = {}
traderlist.sellprice = {}
traderlist.buyprice = {}
traderlistheader = false
traderlistlist = false
traderlist.line = 1
local cargolist = {}
local amount = {}
local limitlist = {}
limitlist.entclass = {}
limitlist.limit = {}
limitlist.currentused = {}
limitlist.line = 1
hudline = 1
surface.CreateFont( "akbar", 20, 500, true, false, "AckBarWriting" );



local vguistring_motd = [["vguistring_motd"
{
	"EditorFrame"
	{
		"ControlName"		"Frame"
		"fieldName"		"EditorFrame"
		"autoResize"		"1"
		"pinCorner"		"0"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
		"settitlebarvisible"		"1"
		"title"		"#MOTD"
		"sizable"		"0"
	}
	"HTMLWindow"
	{
		"ControlName"		"HTML"
		"fieldName"		"HTMLWindow"
		"xpos"		"25"
		"ypos"		"40"
		"zpos"		"290"
		"wide"		"750"
		"tall"		"500"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
		"Default"		"1"
	}
	
}]]

function ShowHelp()
	if (MOTD.Window) then 
		MOTD.Window:SetVisible(true) 
		MOTD.HTML:OpenURL("http://empirephoenix.dyndns.org:81/")
		return 
	end	
	MOTD.Window = vgui.Create("Frame")
    MOTD.Window:SetName("EditorFrame")
    MOTD.HTML = vgui.Create("HTML",MOTD.Window,"HTMLWindow")
    MOTD.Window:LoadControlsFromString( vguistring_motd )
    MOTD.Window:SetKeyBoardInputEnabled( true )
    MOTD.Window:SetMouseInputEnabled( true )
    MOTD.Window:SetPos( 0, 0 )
    MOTD.Window:SetSize(ScrW(),ScrH()/8*7)
	function helppaint()
		MOTD.Window:SetBGColor(50,50,75,60)
	end
	MOTD.Window:SetPaintFunction(helppaint) 
    MOTD.Window:SetVisible( true )
    MOTD.HTML:OpenURL("http://empirephoenix.dyndns.org:81/")
	MOTD.HTML:SetPos( 50, 50 )
	MOTD.HTML:SetSize(ScrW()-100,ScrH()/8*7-100)

end

local cargoholdposition = 1
	amount = {}
	cargolist = {}
function CargoSystem(um )
	local answer = um:ReadString()
	if (answer == "Stop") then
		DisplayCargo()
		cargoholdposition = 1
		amount = {}
		cargolist = {}
	else
		cargolist[cargoholdposition] = answer
		amount[cargoholdposition] = um:ReadLong()
		cargoholdposition = cargoholdposition+1
	end
end
usermessage.Hook("CargoSystem", CargoSystem)  

function DisplayCargo ()
	local lineheigh = 20
	local Buttons = {}
	local Buttons2 = {}
	local Buttons3 = {}
	local Textes = {}
	local Lines = table.getn(cargolist) + 2
	if (Frame) then
        Frame:SetVisible(true)
	else
		local Frame = vgui.Create( "Frame" ); 
	    Frame:SetSize( ScrW()/3, ScrH()/8 * 5 ); 
	    Frame:PostMessage( "SetTitle", "text", "Cargohold");   
		Frame:SetPos( xmenustart ,ymenustart); 
	    Frame:SetVisible( true );
	    Frame:MakePopup( );
			function MyGivePaint()
			Frame:SetBGColor(50,50,75,100)
			end
		Frame:SetPaintFunction(MyGivePaint) 
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Item" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart, yline1+lineheigh);
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Amount" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide-xbuttonwide, yline1+lineheigh);
		
		for Linennummer, line in pairs (cargolist) do
			line = Translate(line)
			Textes[Linennummer] = vgui.Create( "Label", Frame );
			Textes[Linennummer]:SetText( cargolist[Linennummer] );
			Textes[Linennummer]:SetSize( xnamewide, buttonheigh );
			Textes[Linennummer]:SetPos( xnamestart, yline1+lineheigh+lineheigh*Linennummer );
				
			Buttons[Linennummer] = vgui.Create( "Label", Frame );
			Buttons[Linennummer]:SetText( amount[Linennummer] ); 
			Buttons[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons[Linennummer]:SetPos( xnamestart+xnamewide-xbuttonwide, yline1+lineheigh+lineheigh*Linennummer ); 
			
			Buttons2[Linennummer] = vgui.Create( "Button", Frame );
			Buttons2[Linennummer]:SetText( "Eject" ); 
			Buttons2[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons2[Linennummer]:SetPos( xnamestart+xnamewide+xbuttonwide, yline1+lineheigh+lineheigh*Linennummer );
			
			Buttons3[Linennummer] = vgui.Create( "Button", Frame );
			Buttons3[Linennummer]:SetText( "Eject All" ); 
			Buttons3[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons3[Linennummer]:SetPos( xnamestart+xnamewide+xbuttonwide*2, yline1+lineheigh+lineheigh*Linennummer );
				
			
			Buttons3[Linennummer].DoClick = function( self )
				line = TranslateBack(line)
				LocalPlayer():ConCommand("CargoRecive ".. line .. " " .. "all")
				Frame:SetVisible(false)
			end
			Buttons2[Linennummer].DoClick = function( self )
				line = TranslateBack(line)
				LocalPlayer():ConCommand("CargoRecive ".. line .. " " .. "one")
				Frame:SetVisible(false)
			end
		end
			
		ButtonClose = vgui.Create( "Button", Frame );
		ButtonClose:SetText( "Scan" ); 
		ButtonClose:SetSize( xclosebuttonwide, buttonheigh ); 
		ButtonClose:SetPos( xclosebuttonstart, yline1 + lineheigh * Lines); 
		
		function ButtonClose:DoClick()
			LocalPlayer():ConCommand("CargoScan")
			Frame:SetVisible(false)
		end
		
		ButtonClose = vgui.Create( "Button", Frame );
		ButtonClose:SetText( "Close" ); 
		ButtonClose:SetSize( xclosebuttonwide, buttonheigh ); 
		ButtonClose:SetPos( xclosebuttonstart, yline1 + lineheigh * (Lines+1)); 
		
		function ButtonClose:DoClick()
			Frame:SetVisible(false)
		end
	end
end

function DisplayTrade ()
	local lineheigh = 20
	local Buttons = {}
	local Buttons2 = {}
	local Textes = {}
	local Lines = traderlist.line +1
	if (Frame) then
        Frame:SetVisible(true)
	else
		local Frame = vgui.Create( "Frame" ); 
	    Frame:SetSize( ScrW()/3, ScrH()/8 * 5 ); 
	    Frame:PostMessage( "SetTitle", "text", traderlist.header);   
		Frame:SetPos( xmenustart ,ymenustart); 
	    Frame:SetVisible( true );
	    Frame:MakePopup( );
			function MyGivePaint()
			Frame:SetBGColor(50,50,75,100)
			end
		Frame:SetPaintFunction(MyGivePaint) 
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Item" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart, yline1+lineheigh);
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Buy" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide, yline1+lineheigh);
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Sell" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide+xbuttonwide, yline1+lineheigh);
		
		for Linennummer, line in pairs (traderlist.list) do
			line = Translate(line)
			Textes[Linennummer] = vgui.Create( "Label", Frame );
			Textes[Linennummer]:SetText( line );
			Textes[Linennummer]:SetSize( xnamewide, buttonheigh );
			Textes[Linennummer]:SetPos( xnamestart, yline1+lineheigh+lineheigh*Linennummer );
				
			if (traderlist.buyprice[Linennummer] != 0) then
				Buttons[Linennummer] = vgui.Create( "Button", Frame );
				Msg(traderlist.buyprice[Linennummer])
				Buttons[Linennummer]:SetText(traderlist.buyprice[Linennummer]  ); 
				Buttons[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
				Buttons[Linennummer]:SetPos( xnamestart+xnamewide, yline1+lineheigh+lineheigh*Linennummer ); 
			end
			
			if (traderlist.sellprice[Linennummer]  != 0)then
				Buttons2[Linennummer] = vgui.Create( "Button", Frame );
				Buttons2[Linennummer]:SetText( traderlist.sellprice[Linennummer]  ); 
				Buttons2[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
				Buttons2[Linennummer]:SetPos( xnamestart+xnamewide+xbuttonwide, yline1+lineheigh+lineheigh*Linennummer );
			end			
			
			if Buttons[Linennummer] then
				Buttons[Linennummer].DoClick = function( self )
					line = TranslateBack(line)
					Msg (line)
					LocalPlayer():ConCommand("TradeRecive ".. line .. " " .. "Buy")	
				end
			end
			
			if Buttons2[Linennummer] then
				Buttons2[Linennummer].DoClick = function( self )
					line = TranslateBack(line)
					Msg (line)
					LocalPlayer():ConCommand("TradeRecive ".. line .. " " .. "Sell")	
				end
			end
		end
			
			
		ButtonClose = vgui.Create( "Button", Frame );
		ButtonClose:SetText( "Close" ); 
		ButtonClose:SetSize( xclosebuttonwide, buttonheigh ); 
		ButtonClose:SetPos( xclosebuttonstart, yline1+lineheigh*Lines); 
		
		function ButtonClose:DoClick()
			Frame:SetVisible(false)
		end
		traderlist.line = 1
	end
end

function TradeSystem(um )
	local answer = um:ReadString()
	if (answer == "Headerstart") then
		traderlistheader = true
		traderlistlist = false
	elseif (answer == "Liststart") then
		traderlistheader = false
		traderlistlist = true
		traderlist.list = {}
		return
	elseif (answer == "Stop") then
		traderlistheader = false
		traderlistlist = false
		DisplayTrade()
		traderliststuff = nil
		return
	end
	
	if (traderlistheader) then
		traderlist.header = answer
	elseif (traderlistlist) then
		traderlist.list[traderlist.line] = answer
		local answer = um:ReadLong()
		Msg(answer)
		traderlist.sellprice[traderlist.line] = answer
		local answer = um:ReadLong()
		Msg(answer)
		traderlist.buyprice[traderlist.line] = answer
		traderlist.line = traderlist.line + 1
	end
end
usermessage.Hook("TradeSystem", TradeSystem)  


local shopmenulist = {}
shopmenulist["item"] = {}
shopmenulist["price"] = {}
shopmenulist["name"] = ""
function ShopSystem(um )
	local answer	= um:ReadString()
	if (answer == "Start") then
		shopmenulist["item"] = {}
		shopmenulist["price"] = {}
		shopmenulist["name"] = um:ReadString()
	elseif (answer == "Stop") then
		Displayshop()
	elseif (answer == "Item") then
		table.insert(shopmenulist["item"],um:ReadString())
		table.insert(shopmenulist["price"],um:ReadString())
	else
		Msg("Error in SHopsystem")
	end
end
usermessage.Hook("ShopSystem", ShopSystem)  

function Displayshop ()
	local lineheigh = 20
	local Buttons = {}
	local Textes = {}
	local Lines = table.getn(menu)-1
	if (Frame) then
        Frame:SetVisible(true)
	else
		local Frame = vgui.Create( "Frame" ); 
	    Frame:SetSize( ScrW()/3, ScrH()/8 * 5 ); 
	    Frame:PostMessage( "SetTitle", "text",shopmenulist["name"] );   
		Frame:SetPos( xmenustart ,ymenustart); 
	    Frame:SetVisible( true );
	    Frame:MakePopup( );
			function MyGivePaint()
			Frame:SetBGColor(50,50,75,100)
			end
		Frame:SetPaintFunction(MyGivePaint) 
		local anzahllinien = table.getn(shopmenulist["item"])
		for Linennummer, line in pairs (shopmenulist["item"]) do
			Textes[Linennummer] = vgui.Create( "Label", Frame );
			Textes[Linennummer]:SetText(shopmenulist["item"][Linennummer].. " for " .. shopmenulist["price"][Linennummer]);
			Textes[Linennummer]:SetSize( xnamewide*2, buttonheigh );
			Textes[Linennummer]:SetPos( xnamestart, yline1+lineheigh*Linennummer-1 );
				
			Buttons[Linennummer] = vgui.Create( "Button", Frame );
			Buttons[Linennummer]:SetText( "Buy" ); 
			Buttons[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			local xname4wide = xnamewide + 0.5*xnamewide
			Buttons[Linennummer]:SetPos( xnamestart+xname4wide, yline1+lineheigh*Linennummer-1 ); 
			
			Buttons[Linennummer].DoClick = function( self )
			LocalPlayer():ConCommand("ShopRecive ".. shopmenulist["item"][Linennummer])	
		end
		ButtonClose = vgui.Create( "Button", Frame );
		ButtonClose:SetText( "Close" ); 
		ButtonClose:SetSize( xclosebuttonwide, buttonheigh ); 
		ButtonClose:SetPos( xclosebuttonstart, yline1+lineheigh*(anzahllinien+1)); 
		function ButtonClose:DoClick()
			Frame:SetVisible(false)
		end
		end
	end
end

local Weaponlist = {}
Weaponlist[1] = {}
Weaponlist[2] = {}
Weaponlist[3] = {}
Weaponlist[4] = {}
Weaponlist[5] = {}
local allweapons = {}

function registerweaponclass(class)
	table.insert(allweapons,Translate(class))
end

function Weaponconfig(um )
	local answer = um:ReadString()
	local answer2 = um:ReadString()
	if (answer == "Stop") then
		DisplayWeaponconfig()
	elseif (answer == "Start") then
		Weaponlist[1] = {}
		Weaponlist[2] = {}
		Weaponlist[3] = {}
		Weaponlist[4] = {}
		Weaponlist[5] = {}
	elseif (answer == "list1") then
		table.insert(Weaponlist[1],answer2) 
	elseif (answer == "list2") then
		table.insert(Weaponlist[2],answer2) 
	elseif (answer == "list3") then
		table.insert(Weaponlist[3],answer2) 
	elseif (answer == "list4") then
		table.insert(Weaponlist[4],answer2) 
	elseif (answer == "list5") then
		table.insert(Weaponlist[5],answer2) 
	else
		Msg("Error in reciving")
	end
end
usermessage.Hook("Weaponconfig", Weaponconfig)  

function DisplayWeaponconfig ()
	local lineheigh = 20
	local Buttons1 = {}
	local Buttons1text = {}
	local Buttons2 = {}
	local Buttons2text = {}
	local Buttons3 = {}
	local Buttons3text = {}
	local Buttons4 = {}
	local Buttons4text = {}
	local Buttons5 = {}
	local Buttons5text = {}
	local Textes = {}
	local Lines = table.getn(Weaponlist) + 2
	Msg(Lines)
	if (Frame) then
        Frame:SetVisible(true)
	else
		local Frame = vgui.Create( "Frame" ); 
	    Frame:SetSize( ScrW()/2, ScrH()/8 * 5 ); 
	    Frame:PostMessage( "SetTitle", "text", "Weaponconfig");   
		Frame:SetPos( xmenustart ,ymenustart); 
	    Frame:SetVisible( true );
	    Frame:MakePopup( );
			function MyGivePaint()
			Frame:SetBGColor(50,50,75,100)
			end
		Frame:SetPaintFunction(MyGivePaint) 
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Group1" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide, yline1+lineheigh);
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Group2" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide+xbuttonwide, yline1+lineheigh);
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Group3" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide+xbuttonwide*2, yline1+lineheigh);
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Group4" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide+xbuttonwide*3, yline1+lineheigh);
		
		local Table = vgui.Create( "Label", Frame );
		Table:SetText( "Group5" );
		Table:SetSize( xnamewide, buttonheigh );
		Table:SetPos( xnamestart+xnamewide+xbuttonwide*4, yline1+lineheigh);
		
		for Linennummer, line in pairs (allweapons) do
			Textes[Linennummer] = vgui.Create( "Label", Frame );
			local translate = Translate(line)
			Textes[Linennummer]:SetText(translate);
			Textes[Linennummer]:SetSize( xnamewide, buttonheigh );
			Textes[Linennummer]:SetPos( xnamestart, yline1+lineheigh+lineheigh*Linennummer );

			Buttons1[Linennummer] = vgui.Create( "Button", Frame );
			Msg(line)
			Buttons1text[Linennummer] = tostring(table.HasValue(Weaponlist[1],line))
			Buttons1[Linennummer]:SetText( Buttons1text[Linennummer] ); 
			Buttons1[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons1[Linennummer]:SetPos( xnamestart+xnamewide, yline1+lineheigh+lineheigh*Linennummer );
			
			Buttons2[Linennummer] = vgui.Create( "Button", Frame );
			Buttons2text[Linennummer] = tostring(table.HasValue(Weaponlist[2],line))
			Buttons2[Linennummer]:SetText(Buttons2text[Linennummer]); 
			Buttons2[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons2[Linennummer]:SetPos( xnamestart+xnamewide+xbuttonwide, yline1+lineheigh+lineheigh*Linennummer );
		
			Buttons3[Linennummer] = vgui.Create( "Button", Frame );
			Buttons3text[Linennummer] = tostring(table.HasValue(Weaponlist[3],line))
			Buttons3[Linennummer]:SetText(Buttons3text[Linennummer]); 
			Buttons3[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons3[Linennummer]:SetPos( xnamestart+xnamewide+xbuttonwide*2, yline1+lineheigh+lineheigh*Linennummer );
			LocalPlayer():ConCommand("WeaponConfig ".. string.Replace(line," ","!") .. " 3  " .. Buttons3text[Linennummer])
					
			Buttons4[Linennummer] = vgui.Create( "Button", Frame );
			Buttons4text[Linennummer] = tostring(table.HasValue(Weaponlist[4],line))
			Buttons4[Linennummer]:SetText(Buttons4text[Linennummer]); 
			Buttons4[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons4[Linennummer]:SetPos( xnamestart+xnamewide+xbuttonwide*3, yline1+lineheigh+lineheigh*Linennummer );
		
			Buttons5[Linennummer] = vgui.Create( "Button", Frame );
			Buttons5text[Linennummer] = tostring(table.HasValue(Weaponlist[5],line))
			Buttons5[Linennummer]:SetText(Buttons5text[Linennummer]); 
			Buttons5[Linennummer]:SetSize( xbuttonwide, buttonheigh ); 
			Buttons5[Linennummer]:SetPos( xnamestart+xnamewide+xbuttonwide*4, yline1+lineheigh+lineheigh*Linennummer );
			
			Buttons1[Linennummer].DoClick = function( self )
				if (Buttons1text[Linennummer] == "true") then
					Buttons1text[Linennummer] = "false"
				else
					Buttons1text[Linennummer] = "true"
				end
				LocalPlayer():ConCommand("WeaponConfig ".. line .. " " .. 1 .. " " .. Buttons1text[Linennummer])
				Buttons1[Linennummer]:SetText( Buttons1text[Linennummer] )
			end
			
			Buttons2[Linennummer].DoClick = function( self )
				if (Buttons2text[Linennummer] == "true") then
					Buttons2text[Linennummer] = "false"
				else
					Buttons2text[Linennummer] = "true"
				end
				LocalPlayer():ConCommand("WeaponConfig ".. line .. " " .. 2 .. " " .. Buttons2text[Linennummer])
				Buttons2[Linennummer]:SetText( Buttons2text[Linennummer] )
			end
						
			Buttons3[Linennummer].DoClick = function( self )
				if (Buttons3text[Linennummer] == "true") then
					Buttons3text[Linennummer] = "false"
				else
					Buttons3text[Linennummer] = "true"
				end
				LocalPlayer():ConCommand("WeaponConfig ".. line .. " " .. 3 ..  " " .. Buttons3text[Linennummer])
				Buttons3[Linennummer]:SetText( Buttons3text[Linennummer] )
			end
						
			Buttons4[Linennummer].DoClick = function( self )
				if (Buttons4text[Linennummer] == "true") then
					Buttons4text[Linennummer] = "false"
				else
					Buttons4text[Linennummer] = "true"
				end
				LocalPlayer():ConCommand("WeaponConfig ".. line .. " " .. 4 ..  " " ..  Buttons4text[Linennummer])
				Buttons4[Linennummer]:SetText( Buttons4text[Linennummer] )
			end
						
			Buttons5[Linennummer].DoClick = function( self )
				if (Buttons5text[Linennummer] == "true") then
					Buttons5text[Linennummer] = "false"
				else
					Buttons5text[Linennummer] = "true"
				end
				LocalPlayer():ConCommand("WeaponConfig ".. line .. " " .. 5 ..  " " .. Buttons5text[Linennummer])
				Buttons5[Linennummer]:SetText( Buttons5text[Linennummer] )
			end
		end
			
		ButtonClose = vgui.Create( "Button", Frame );
		ButtonClose:SetText( "Close" ); 
		ButtonClose:SetSize( xclosebuttonwide, buttonheigh ); 
		ButtonClose:SetPos( xclosebuttonstart, yline1 + lineheigh + (lineheigh * (table.getn(allweapons)+1))) 
		
		function ButtonClose:DoClick()
			Frame:SetVisible(false)
		end
	end
end


function Limitsystem(um )
	local answer = um:ReadString()
	if (answer == "Stop") then
		DisplayLimitmenu()
		limitlist.line = 1
	else
		limitlist.entclass[limitlist.line] = answer
		answer = um:ReadLong()
		limitlist.limit[limitlist.line] = answer
		answer = um:ReadLong()
		limitlist.currentused[limitlist.line] = answer
		limitlist.line = limitlist.line +1
	end
end
usermessage.Hook("Limitmenu", Limitsystem)  

function DisplayLimitmenu( )
	if (Frame) then
        Frame:SetVisible(true)
	else
		local Frame = vgui.Create( "Frame" ); 
	    Frame:SetSize( ScrW()/3, ScrH()/8*7 ); 
	    Frame:PostMessage( "SetTitle", "text", "Your Limits" );   
		Frame:SetPos( 0 ,ymenustart); 
	    Frame:SetVisible( true );
	    Frame:MakePopup( ); 
			function MyGivePaint()
			Frame:SetBGColor(50,50,75,60)
			end
		Frame:SetPaintFunction(MyGivePaint) 
		local Name = {}
		for k, i in pairs (limitlist.entclass) do
			Name[k] = vgui.Create( "Label", Frame );
			Name[k]:SetText(i .. " " .. limitlist.currentused[k]-1 .. "/" .. limitlist.limit[k]);
			Name[k]:SetSize( xnamewide*4, buttonheigh );
			Name[k]:SetPos( xnamestart, yline1+lineheigh*k );
		end
		
		local Buttonclose = vgui.Create( "Button", Frame );
		Buttonclose:SetText( "Reset" ); 
		Buttonclose:SetSize( xclosebuttonwide, buttonheigh ); 
		Buttonclose:SetPos( xclosebuttonstart, yline1+lineheigh*(table.getn(limitlist.entclass)+1)); 
		
		function Buttonclose:DoClick()
			LocalPlayer():ConCommand("Limitmenu true")					
			Frame:SetVisible(false)
		end
		
	end
end

function OpenDeathMenu ()
	local Frame = vgui.Create( "Frame" ); 
	Frame:SetSize( ScrW()/3, ScrH()/8 * 5 ); 
	Frame:PostMessage( "SetTitle", "text", "Respawn Menu" );   
	Frame:SetPos( xmenustart ,ymenustart); 
	Frame:SetVisible( true );
	Frame:MakePopup( );
	function MyGivePaint()
		Frame:SetBGColor(50,50,75,100)
	end
	Frame:SetPaintFunction(MyGivePaint) 
	Button1 = vgui.Create( "Button", Frame );
	Button1:SetText("Respawn in Ship 30$" ); 
	Button1:SetSize( ScrW()/3,(ScrH()/8 * 4  )/2 ); 
	Button1:SetPos( xmenustart,ymenustart); 
	
	Button1.DoClick = function( self )
		LocalPlayer():ConCommand("Respawnanswer false")
		Frame:SetVisible(false)
	end
	
	Button2 = vgui.Create( "Button", Frame );
	Button2:SetText("Respawn in Startstation" ); 
	Button2:SetSize( ScrW()/3,(ScrH()/8 * 4  )/2 ); 
	Button2:SetPos( xmenustart,ymenustart+(ScrH()/8 * 4  )/2 )
	
	Button2.DoClick = function( self )
		LocalPlayer():ConCommand("Respawnanswer")
		Frame:SetVisible(false)
		
	end
end
usermessage.Hook("OpenDeathMenu", OpenDeathMenu) 

function FriendEnemy( )
	local firstPlayers = player.GetAll( );
	local Players = {}
	local LocalPlayer = LocalPlayer()
	local Index = 1
	local NumberofPlayers = 0
	for k, i in pairs (firstPlayers) do
		if (i != LocalPlayer) then
			Players[Index] = i
			Index = Index +1
		end
	end

	if (Frame) then
        Frame:SetVisible(true)
	else
		local Frame = vgui.Create( "Frame" ); 
	    Frame:SetSize( ScrW()/3, ScrH()/8 * 5 ); 
	    Frame:PostMessage( "SetTitle", "text", "Enemy - Friend Configure" );   
		Frame:SetPos( xmenustart ,ymenustart); 
	    Frame:SetVisible( true );
	    Frame:MakePopup( ); 
			function MyGivePaint()
			Frame:SetBGColor(50,50,75,60)
			end
		Frame:SetPaintFunction(MyGivePaint) 
		local Name = {}
		local Buttonneutral = {}
		local Buttonenemy = {}
		local Buttonfriendly = {}
		local Finallist = {}
		
		for k, i in pairs (Players) do
			Finallist[k] = {}
			Name[k] = vgui.Create( "Label", Frame );
			Name[k]:SetText( i:GetName( ) );
			Name[k]:SetSize( xnamewide, buttonheigh );
			Name[k]:SetPos( xnamestart, yline1+lineheigh*k );
				
			Buttonenemy[k] = vgui.Create( "Button", Frame );
			Buttonenemy[k]:SetText( "Enemy" ); 
			Buttonenemy[k]:SetSize( xbuttonwide, buttonheigh ); 
			Buttonenemy[k]:SetPos( xbuttonenemystart, yline1+lineheigh*k ); 
			Buttonenemy[k].pl = i
			
			Buttonneutral[k] = vgui.Create( "Button", Frame );
			Buttonneutral[k]:SetText( "!Neutral!" ); 
			Buttonneutral[k]:SetSize( xbuttonwide, buttonheigh ); 
			Buttonneutral[k]:SetPos( xbuttonneutralstart, yline1+lineheigh*k ); 
			Buttonneutral[k].pl = i
			Finallist[k][1] = i
			Finallist[k][2] = 2
			
			Buttonfriendly[k] = vgui.Create( "Button", Frame );
			Buttonfriendly[k]:SetText( "Friend" ); 
			Buttonfriendly[k]:SetSize( xbuttonwide, buttonheigh ); 
			Buttonfriendly[k]:SetPos( xbuttonfriendlystart, yline1+lineheigh*k ); 
			Buttonfriendly[k].pl = i
		end
		
		local Buttonclose = vgui.Create( "Button", Frame );
			Buttonclose:SetText( "Apply" ); 
			Buttonclose:SetSize( xclosebuttonwide, buttonheigh ); 
			Buttonclose:SetPos( xclosebuttonstart, yline1+lineheigh*(table.getn(Players)+1)); 
			function Buttonclose:DoClick()
				Enemylist = {}
				Neutrallist = {}
				Friendlist = {}
				local Enemynumber = 1
				local Friendnumber = 1
				local Neutralnumber = 1
				for k,v in pairs(Finallist) do
					if (Finallist[k][2] == 1) then
						Enemylist[Enemynumber] = Finallist[k][1]
						Enemynumber = Enemynumber +1
					elseif (Finallist[k][2] == 2) then
						Neutrallist[Neutralnumber] = Finallist[k][1]
						Neutralnumber = Neutralnumber +1
					elseif (Finallist[k][2] == 3) then
						Friendlist[Friendnumber] = Finallist[k][1]
						Friendnumber = Friendnumber +1
					end
				end
				
				LocalPlayer:ConCommand("FriendEnemy Stop")					
				LocalPlayer:ConCommand("FriendEnemy Enemylist")	
				for k,v in pairs(Enemylist) do
					LocalPlayer:ConCommand("FriendEnemy " .. Enemylist[k]:EntIndex( ))	
				end
				LocalPlayer:ConCommand("FriendEnemy Stop")	
				
				LocalPlayer:ConCommand("FriendEnemy Friendlist")	
				for k,v in pairs(Friendlist) do
					LocalPlayer:ConCommand("FriendEnemy " .. Friendlist[k]:EntIndex( ))	
				end
				LocalPlayer:ConCommand("FriendEnemy " .. LocalPlayer:EntIndex( ))
				LocalPlayer:ConCommand("FriendEnemy Stop")	
				
				Frame:SetVisible(false)
			end
			
		for k,v in pairs(Buttonenemy) do
			function v:DoClick()
				Buttonenemy[k]:SetText( "!Enemy!" ); 
				Buttonneutral[k]:SetText( "Neutral" ); 
				Buttonfriendly[k]:SetText( "Friend" ); 
				Finallist[k][2] = 1
			end
		end 
		
		for k,v in pairs(Buttonneutral) do
			function v:DoClick()
				Buttonenemy[k]:SetText( "Enemy" ); 
				Buttonneutral[k]:SetText( "!Neutral!" ); 
				Buttonfriendly[k]:SetText( "Friend" );  
				Finallist[k][2] = 2
			end
		end 
			
		for k,v in pairs(Buttonfriendly) do
			function v:DoClick()
				Buttonenemy[k]:SetText( "Enemy" ); 
				Buttonneutral[k]:SetText( "Neutral" ); 
				Buttonfriendly[k]:SetText( "!Friend!" ); 
				Finallist[k][2] = 3
			end
		end 
	end
end

function Interaction()
	local startmenux2 = ScrW() / 10*4
	local startmenuxwide = ScrW() / 10*2
	local startmenux = 0
	local startmenuy = Screenheigh/16*12.5
	local widemenu = ScrW()
	local heighmenu = Screenheigh/16*1
	local heighbutton = Screenheigh/16*0.5
	FrameInteraction = vgui.Create( "Panel" ); 
    FrameInteraction:SetSize(startmenuxwide,heighmenu); 
	FrameInteraction:SetPos( startmenux2 ,startmenuy); 
    FrameInteraction:SetVisible( true );
    FrameInteraction:MakePopup( );
	function MyGivePaint()
		FrameInteraction:SetBGColor(50,50,75,60)
	end
	FrameInteraction:SetPaintFunction(MyGivePaint) 

	ButtonBuy = vgui.Create( "Button", FrameInteraction );
	ButtonBuy:SetText( "Shop" ); 
	ButtonBuy:SetSize( startmenuxwide, heighbutton ); 
	ButtonBuy:SetPos( 0, 0); 
	function ButtonBuy:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		local LocalPlayer = LocalPlayer()
		LocalPlayer:ConCommand("ShopInitialize")	--sends the server a command to stat the shop
 	end
	
	ButtonTrade = vgui.Create( "Button", FrameInteraction );
	ButtonTrade:SetText( "Trade" ); 
	ButtonTrade:SetSize( startmenuxwide, heighbutton ); 
	ButtonTrade:SetPos( 0, heighbutton); 
	function ButtonTrade:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		local LocalPlayer = LocalPlayer()
		LocalPlayer:ConCommand("TradeInitialize")	
 	end
end

function RpMenu()
	local startmenux1 = ScrW() / 10*6
	local startmenuxwide = ScrW() / 10*2
	local startmenux = 0
	local startmenuy = Screenheigh/16*10.5
	local widemenu = ScrW()
	local heighmenu = Screenheigh/16*3
	local heighbutton = Screenheigh/16*0.5
	FrameRpMenu = vgui.Create( "Panel" ); 
    FrameRpMenu:SetSize(startmenuxwide,heighmenu); 
	FrameRpMenu:SetPos( startmenux1 ,startmenuy); 
    FrameRpMenu:SetVisible( true );
    FrameRpMenu:MakePopup( );
	function MyGivePaint()
		FrameRpMenu:SetBGColor(50,50,75,60)
	end
	FrameRpMenu:SetPaintFunction(MyGivePaint) 

	ButtonDrop = vgui.Create( "Button", FrameRpMenu );
	ButtonDrop:SetText( "Kill Music " ); 
	ButtonDrop:SetSize( startmenuxwide, heighbutton ); 
	ButtonDrop:SetPos( 0, heighbutton); 
	function ButtonDrop:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		local Table = {11}
		MusicPlay(0,0,Table)
		local LocalPlayer = LocalPlayer()
		LocalPlayer:ConCommand("stopsounds")	
 	end
	
	ButtonVoteCop = vgui.Create( "Button", FrameRpMenu );
	ButtonVoteCop:SetText( "Config Weapons" ); 
	ButtonVoteCop:SetSize( startmenuxwide, heighbutton ); 
	ButtonVoteCop:SetPos( 0, heighbutton*2); 
	function ButtonVoteCop:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		local LocalPlayer = LocalPlayer()
		LocalPlayer:ConCommand("Weaponconfigstart")	
 	end
	
	ButtonVoteCitizen = vgui.Create( "Button", FrameRpMenu );
	ButtonVoteCitizen:SetText( "Limitmenu" ); 
	ButtonVoteCitizen:SetSize( startmenuxwide, heighbutton ); 
	ButtonVoteCitizen:SetPos( 0, heighbutton*3); 
	function ButtonVoteCitizen:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		local LocalPlayer = LocalPlayer()
		LocalPlayer:ConCommand("Limitmenu")	
 	end
	
	ButtonAddOwner = vgui.Create( "Button", FrameRpMenu );
	ButtonAddOwner:SetText( "Cargosystem" ); 
	ButtonAddOwner:SetSize( startmenuxwide, heighbutton ); 
	ButtonAddOwner:SetPos( 0, heighbutton*4); 
	function ButtonAddOwner:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		LocalPlayer():ConCommand("CargoStart")
 	end
	
	ButtonType = vgui.Create( "Button", FrameRpMenu );
	ButtonType:SetText( "Jump" ); 
	ButtonType:SetSize( startmenuxwide, heighbutton ); 
	ButtonType:SetPos( 0, heighbutton*5); 
	function ButtonType:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		LocalPlayer():ConCommand("Jump")
 	end
end

function OpenMenu()
	if (FrameMenu) then FrameMenu:SetVisible(true) return end
	
	local startmenux1 = ScrW() / 10*2
	local startmenux2 = ScrW() / 10*4
	local startmenux3 = ScrW() / 10*6
	local startmenux4 = ScrW() / 10*8
	local startmenuxwide = ScrW() / 10*2
	local startmenux = 0
	local startmenuy = Screenheigh/16*13.5
	local widemenu = ScrW()
	local heighmenu = Screenheigh/16*0.5
	
	FrameMenu = vgui.Create( "Panel" ); 
    FrameMenu:SetSize(widemenu,heighmenu); 
	FrameMenu:SetPos( startmenux ,startmenuy); 
    FrameMenu:SetVisible( true );
    FrameMenu:MakePopup( );
	function MyGivePaint()
		FrameMenu:SetBGColor(50,50,75,60)
	end
	FrameMenu:SetPaintFunction(MyGivePaint) 

	ButtonInteraction = vgui.Create( "Button", FrameMenu );
	ButtonInteraction:SetText( "Interact" ); 
	ButtonInteraction:SetSize( startmenuxwide, heighmenu ); 
	ButtonInteraction:SetPos( startmenux2, 0); 
	function ButtonInteraction:DoClick()
		if not (FrameInteraction) then
			Interaction()
		else
			FrameInteraction:SetVisible(true)
		end
	end

	ButtonRP = vgui.Create( "Button", FrameMenu );
	ButtonRP:SetText( "Ship" ); 
	ButtonRP:SetSize( startmenuxwide, heighmenu ); 
	ButtonRP:SetPos( startmenux3, 0); 
	function ButtonRP:DoClick()
		if not (FrameRpMenu) then
			RpMenu()
		else
			FrameRpMenu:SetVisible(true)	
		end
	end
	
	ButtonCancel = vgui.Create( "Button", FrameMenu );
	ButtonCancel:SetText( "Chancel" ); 
	ButtonCancel:SetSize( startmenuxwide, heighmenu ); 
	ButtonCancel:SetPos( startmenux4, 0); 
	function ButtonCancel:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
	end
	
	ButtonHelp = vgui.Create( "Button", FrameMenu );
	ButtonHelp:SetText( "HELP" ); 
	ButtonHelp:SetSize( startmenuxwide, heighmenu ); 
	ButtonHelp:SetPos( 0, 0); 
	function ButtonHelp:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		ShowHelp()
	end

	ButtonFriendEnemy = vgui.Create( "Button", FrameMenu );
	ButtonFriendEnemy:SetText( "Friend Enemy" ); 
	ButtonFriendEnemy:SetSize( startmenuxwide, heighmenu ); 
	ButtonFriendEnemy:SetPos( startmenux1, 0); 
	function ButtonFriendEnemy:DoClick()
		if (FrameMenu) then
			FrameMenu:SetVisible(false)
		end
		if (FrameInteraction) then
			FrameInteraction:SetVisible(false)
		end
		if (FrameRpMenu) then
			FrameRpMenu:SetVisible(false)
		end
		FriendEnemy()
	end
end
usermessage.Hook("OpenMenu", OpenMenu) 